﻿using System;
using fastCSharp.code.cSharp;
using System.Collections.Generic;
using System.Threading;
using System.Linq.Expressions;

namespace fastCSharp.sql.cache.whole.events
{
    /// <summary>
    /// 关键字整表缓存
    /// </summary>
    /// <typeparam name="valueType">表格绑定类型</typeparam>
    /// <typeparam name="modelType">表格模型类型</typeparam>
    /// <typeparam name="keyType">关键字类型</typeparam>
    public abstract class key<valueType, modelType, memberCacheType, keyType> : cache<valueType, modelType, memberCacheType>
        where valueType : class, modelType
        where modelType : class
        where keyType : IEquatable<keyType>
        where memberCacheType : class
    {
        /// <summary>
        /// 键值获取器
        /// </summary>
        internal Func<modelType, keyType> GetKey { get; private set; }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="key">关键字</param>
        /// <returns>数据</returns>
        public abstract valueType this[keyType key] { get; }
        /// <summary>
        /// 获取成员缓存
        /// </summary>
        public readonly Func<keyType, memberCacheType> GetMemberCacheByKey;
        /// <summary>
        /// 关键字整表缓存
        /// </summary>
        /// <param name="sqlTool">SQL操作工具</param>
        /// <param name="getKey">键值获取器</param>
        /// <param name="group">数据分组</param>
        public key(fastCSharp.emit.sqlTable.sqlTool<valueType, modelType> sqlTool, Expression<Func<valueType, memberCacheType>> memberCache, Func<modelType, keyType> getKey, int group = 0)
            : base(sqlTool, memberCache, group)
        {
            if (getKey == null) log.Error.Throw(log.exceptionType.Null);
            GetKey = getKey;
            GetMemberCacheByKey = getMemberCacheByKey;
        }
        /// <summary>
        /// 获取成员缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        internal memberCacheType getMemberCacheByKey(keyType key)
        {
            valueType value = this[key];
            return value != null ? base.GetMemberCache(value) : null;
        }
    }

}
